﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

namespace WSweb
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "WSweb - Admin";
            if ((string)Session["korisnik"] != "admin")
            {
                Response.Redirect("Nedozvoljena_autorizacija.aspx");
            }
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
        }

        protected void btnOcisti_Click(object sender, EventArgs e)
        {
            txt_imeVijesti.Text = "";
            txt_sadrzajVijesti.Text = "";
            ltlVijesti.Text = "";
        }

        protected void btnUnesi_Click(object sender, EventArgs e)
        {
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append("<div style='border:2px dashed #aeaeae; padding:0 15px 15px 15px;'>");
            sb.Append("<span style='font-size:30px;'>" + txt_imeVijesti.Text + "</span>");
            sb.Append("<p>  <img src='images/no_image.gif' height='100px' alt='no image' /></p>");
            sb.Append("<p>" + txt_sadrzajVijesti.Text + "</p>");
            sb.Append("<span style='font-size:12px;font-style:italic;color:#aeaeae;'>Dodano: " +
                DateTime.Now.ToString() + " | " + Session["korisnik"].ToString() + "</span>");
            sb.Append("</div>");
            ltlVijesti.Text = sb.ToString();
            
        }

        protected void btnSpremi_Click(object sender, EventArgs e)
        {
            if (txt_imeVijesti.Text != "" && txt_sadrzajVijesti.Text != "")
            {
                int image_id = 1;
                int image_height = 250;
                string image_title = "no_image.gif";
                SqlConnection c = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["DBwswebCS"].ConnectionString);
                c.Open();
                SqlCommand comm, comm2,comm3;
                string image_type = "image/gif";
                comm = new SqlCommand("insert into Vijest values (@naziv,@sadrzaj,@datum,@username,@image,@image_title,@image_id,@image_type,@image_height)", c);
                byte[] imageBytes;
                comm3 = new SqlCommand("select top 1 image from Vijest where image_id=1",c);
                SqlDataReader sdr = comm3.ExecuteReader();
                sdr.Read();
                imageBytes = (Byte[])sdr["image"];
                sdr.Close();
                if (fuImage.HasFile != false)
                {
                    imageBytes = new byte[fuImage.PostedFile.InputStream.Length + 1];
                    fuImage.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length);
                    string fileName = Server.HtmlEncode(fuImage.FileName);
                    string extension = System.IO.Path.GetExtension(fileName);
                    System.Drawing.Image UploadedImage = System.Drawing.Image.FromStream(fuImage.PostedFile.InputStream);
                    if (extension.EndsWith("png"))
                        image_type = "image/png";
                    else if (extension.EndsWith("jpg"))
                        image_type = "image/jpg";
                    else if (extension.EndsWith("bmp"))
                        image_type = "image/bmp";
                    else
                        image_type = "image/gif";
                    comm2 = new SqlCommand("select image_id from Vijest order by 1 desc", c);
                    image_id = (int)comm2.ExecuteScalar() + 1;
                    image_title = fuImage.PostedFile.FileName;
                    image_height = (int)UploadedImage.PhysicalDimension.Height;
                }
                comm.Parameters.AddWithValue("@naziv", txt_imeVijesti.Text);
                comm.Parameters.AddWithValue("@sadrzaj", txt_sadrzajVijesti.Text);
                comm.Parameters.AddWithValue("@datum", DateTime.Now);
                comm.Parameters.AddWithValue("@username", Session["korisnik"].ToString());
                comm.Parameters.AddWithValue("@image", imageBytes);
                comm.Parameters.AddWithValue("@image_title", image_title);
                comm.Parameters.AddWithValue("@image_id", image_id);
                comm.Parameters.AddWithValue("@image_type", image_type);
                comm.Parameters.AddWithValue("@image_height", image_height);
                int broj = comm.ExecuteNonQuery();
                if (broj == 0)
                {
                    ltlVijesti.Text = "<h2>Neuspješno dodavanje vijesti u bazu!</h2>";
                }
                c.Close();
            }
            else
                ltlVijesti.Text = "<h2>Naziv i sadržaj vijesti ne smiju biti prazni!</h2>";
        }
    }
}